package com.kaiyidiannao.common.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by 闯儿 on 14-4-3.
 */
public class UploadImageUtils {

    /* 请求码 */
    public static final int IMAGE_REQUEST_CODE = 0;
    public static final int CAMERA_REQUEST_CODE = 1;
    public static final int RESULT_REQUEST_CODE = 2;
//    public static int photoIndex = 0;
//    public static Cursor cursor;
//    public static ArrayList<String> photoPathList = new ArrayList<String>();
    /**
     * 裁剪图片方法实现
     *
            * @param uri
    */
    public static void startPhotoZoom(Activity activity,Uri uri) {
//        String columns[] = new String[]{
//                MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.Media.TITLE, MediaStore.Images.Media.DISPLAY_NAME
//        };

        //cursor = this.managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
//        cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
//        photoIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//
//        if(cursor.moveToFirst()){
//            photoPathList.add(showImage());
//        }
//        for (int i = 0; i<cursor.getColumnCount();i++){
//            if(cursor.moveToNext()){
//                photoPathList.add(showImage());
//            }
//            Log.e("photoPath",photoPathList.get(i).toString());
//        }

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(uri.toString(),options);
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        Log.d("uri",uri.toString());
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 340);
        intent.putExtra("outputY", 340);
        intent.putExtra("return-data", true);
        activity.startActivityForResult(intent, RESULT_REQUEST_CODE);

    }

//    public static String photoPath;
//    private static String showImage(){
//        photoPath = cursor.getString(photoIndex);
//        return photoPath;
//    }

    /**
     * 保存裁剪之后的图片数据
     *
     * @param data
     */
    public static Drawable getImageToView(Intent data, Context context) {
        Bundle extras = data.getExtras();
        Drawable drawable = null;
        if (extras != null) {
            Log.d("image_path",extras.getString("img_path"));
//            Bitmap photo = extras.getParcelable("data");
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Bitmap photo = BitmapFactory.decodeFile(extras.getString("img_path"), options);

            drawable = new BitmapDrawable(context.getResources(),photo);
        }
        return  drawable;
    }
}
